package com.example.controller;

import com.example.annotation.GlobalInterceptor;
import com.example.entity.constants.Constants;
import com.example.entity.dto.TokenUserInfoDTO;
import com.example.entity.po.UserInfo;
import com.example.entity.vo.ResponseVO;
import com.example.entity.vo.UserInfoVO;
import com.example.exception.BusinessException;
import com.example.redis.RedisComponent;
import com.example.redis.RedisUtils;
import com.example.service.UserInfoService;
import com.example.utils.CopyTools;
import com.wf.captcha.ArithmeticCaptcha;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.constraints.Email;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/account")
@Validated
public class AccountController extends ABaseController{

    private static final Logger logger =   LoggerFactory.getLogger(AccountController.class);

    @Resource
    private RedisUtils redisUtils;
    @Resource
    private RedisComponent redisComponent;
    @Resource
    private UserInfoService userInfoService;

    /**
     * 图片验证码
     * @return
     */
    @RequestMapping("/checkCode")
    public ResponseVO checkCode() {
        ArithmeticCaptcha captcha = new ArithmeticCaptcha(100,42);
        String code = captcha.text();
        String checkCodeKey = UUID.randomUUID().toString();
        redisUtils.setex(Constants.REDIS_KEY_CHECK_CODE + checkCodeKey,code,Constants.REDIS_TIME_1MIN * 10);
        String checkCodeBase64 = captcha.toBase64();
        logger.info("验证码{}" ,code);
        logger.info("验证码" +code);
        Map<String,String> result = new HashMap<>();
        result.put("checkCodeBase64",checkCodeBase64);
        result.put("checkCodeKey",checkCodeKey);
        return getSuccessResponseVO(result);
    }

    /**
     * 注册
     * @return
     */
    @RequestMapping("/register")
    public ResponseVO register(@NotEmpty String checkCodeKey,
                               @NotEmpty @Email String email,
                               @NotEmpty String password,
                               @NotEmpty String nickName,
                               @NotEmpty String checkCode) {
        try {
            if (!checkCode.equalsIgnoreCase((String) redisUtils.get(Constants.REDIS_KEY_CHECK_CODE + checkCodeKey))) {
                throw new BusinessException("图片验证码不正确！");
            }

            userInfoService.register(email,nickName,password);
            return getSuccessResponseVO(null);

        } catch (BusinessException e) {
            throw new RuntimeException(e);
        } finally {

            redisUtils.delete(Constants.REDIS_KEY_CHECK_CODE + checkCodeKey);
        }

    }

    /**
     * 登录
     * @return
     */
    @RequestMapping("/login")
    public ResponseVO login(@NotEmpty String checkCodeKey,
                               @NotEmpty @Email String email,
                               @NotEmpty String password,
                               @NotEmpty String checkCode) {
        try {
            if (!checkCode.equalsIgnoreCase((String) redisUtils.get(Constants.REDIS_KEY_CHECK_CODE + checkCodeKey))) {
                throw new BusinessException("图片验证码不正确！");
            }
            UserInfoVO userInfoVO = userInfoService.login(email, password);

            return getSuccessResponseVO(userInfoVO);
        } catch (BusinessException e) {
            throw new RuntimeException(e);
        } finally {
            redisUtils.delete(Constants.REDIS_KEY_CHECK_CODE + checkCodeKey);
        }

    }

    @RequestMapping("/getSysSetting")
    @GlobalInterceptor
    public ResponseVO login() {
        return getSuccessResponseVO(redisComponent.getSysSetting());
    }
}
